package cn.spring.ly.config;

import java.sql.Timestamp;
import java.time.LocalDateTime;

import javax.annotation.Resource;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;

import com.jagregory.shiro.freemarker.ShiroTags;

import cn.springfm.test.ShiroTest;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.SimpleDate;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;

@Configuration
public class FreeMarkerConverConfig {

	@Resource
	public void configureFreemarkerConfigurer(FreeMarkerConfig configurer) {
		configurer.getConfiguration().setSharedVariable("shiro", new ShiroTags());
		configurer.getConfiguration().setObjectWrapper(new CustomObjectWrapper());
	}

	/**
	 * 自定义类型转换
	 * 
	 * @author acer
	 *
	 */
	private static class CustomObjectWrapper extends DefaultObjectWrapper {
		@Override
		public TemplateModel wrap(Object obj) throws TemplateModelException {
			if (obj instanceof LocalDateTime) {
				Timestamp timestamp = Timestamp.valueOf((LocalDateTime) obj);
				return new SimpleDate(timestamp);
			}

			return super.wrap(obj);
		}
	}
}
